/*
 * display_tool.cpp
 *
 *  Created on: 2016年6月26日
 *      Author: guyadong
 */
#include "display_tool.h"
#include "CImgWrapper.h"
namespace gdface{
cimg_library::CImgWrapper<uint8_t> toCImg(const gray_matrix_cl& img,bool original=true,bool is_shared=false){
	if(img.get_row_stride()==img.width||original){
		return cimg_library::CImgWrapper<uint8_t>(img.v.data(), img.get_row_stride(), img.height, 1, 1,is_shared);
	}else{
		// 去掉多余的对齐边数据
		std::vector<uint8_t> tmp(img.width*img.height);
		auto row_stride=img.get_row_stride();
		auto src_ptr=img.v.data();
		auto dst_ptr=tmp.data();
		auto end_ptr=src_ptr+img.v.size();
		for(;src_ptr<end_ptr;src_ptr+=row_stride,dst_ptr+=img.width){
			memcpy(dst_ptr,src_ptr,img.width);
		}
		return cimg_library::CImgWrapper<uint8_t>(tmp.data(), img.width, img.height, 1, 1,false);
	}
}
/*
 * 在图像上显示检测到的目标
 * */
void show_detect_objects_on_img(gray_matrix_cl& img,
		struct_cl<detected_objects_buffer_norm>& detect_obj, size_t from, size_t to) {
	if (!global_run_config.debug_show)
		return;

	tls::command_queue().finish();
	detect_obj.download_if_need();
	throw_if(from > size_t(detect_obj.detected_num))
	if (0 == to)
		to = detect_obj.detected_num;

	throw_if(from > to || to > size_t(detect_obj.detected_num))
	img.download_if_need();
	auto cimg = toCImg(img,false);
	unsigned char color[] = { 255 };
	for (auto i = from; i < to; ++i) {
		auto obj_info = detect_obj.storage[i];
		cl_int2 pos { int(obj_info.x * img.width), int(obj_info.y * img.height) }; // 归一化坐标转为基于当前图像尺寸的坐标
		auto sum = cl_int(obj_info.z);
		auto detect_size = int(obj_info.w * std::min(img.width, img.height));
		cimg.draw_rectangle(pos.x, pos.y, int(pos.x + detect_size), int(pos.y + detect_size), color, 0.9f, ~0U);
	}
	if (from < to)
		cimg.display();
}
/*
 * 显示图像
 */
void display_gray_matrix_cl(const gray_matrix_cl &img,bool original,const std::string& title, const bool display_info, unsigned int *const XYZ,
        const bool exit_on_anykey){
	toCImg(img,original,true).display(title.empty()?nullptr:title.data(),display_info,XYZ,exit_on_anykey);
}
/*
* 显示hit_mat
* */
void show_hit_mat(matrix_cl<cl_uchar, cl::Buffer>& hit_mat) {
	if (!global_run_config.debug_show)
		return;

	tls::command_queue().finish();
	hit_mat.download_if_need();
	gray_matrix_cl(hit_mat).display(false);
}
}/*namespace gdface*/



